/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmListadoSubcategoria.java
 *
 * Created on 12/01/2012, 01:34:06 AM
 */

package Categorias;

import Beans.SubcategoriaBean;
import Blogic.SubcategoriaBLogic;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author orellana
 */
public class frmListadoSubcategoria extends javax.swing.JFrame {
     // Atributos de la clase
    private String col[]={"Nombre", "Descripcion", "idSubcategoria"};
    public DefaultTableModel modelSubcategorias=new DefaultTableModel(col,0);
    private String subcategoriaSel="";
    frmAltaSubcategoria altaSubcategoria=null;

    public frmListadoSubcategoria() {
        initComponents();
        //Definimos posicion del formulario
        this.setLocationRelativeTo(null);
        //Definimos cerrado del formulario
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);

        // Definimos el tamaño de las columnas (Tabla)
        getTablaListadoSubcategoria();
        // Obtenemos subcategorias
        obtSubcategorias();
    }

    // Obtiene subcategoria registradas
    public void obtSubcategorias(){
    // Variables locales
    Vector datosSubcategorias=new Vector();
    SubcategoriaBean sqlBean=new SubcategoriaBean();
    // Obtenemos cantidad de registro de la tabla
    int count = modelSubcategorias.getRowCount();
    // Hacemos un ciclo for para hacer la comparacion
    for(int r=0; r<count;r++){
        // Eliminamos los datos que contenga la tabla
        modelSubcategorias.removeRow(0);
    }
    // Realizamos la consulta
    datosSubcategorias=(new SubcategoriaBLogic()).getDatosSubcategorias(sqlBean);
    // Validamos registros encontrados
    if (!datosSubcategorias.isEmpty()){
        // Vaciamos los datos en la tabla
        // Ciclo for
        for (int i=0; i<datosSubcategorias.size();i++){
            SubcategoriaBean detalleDatosSubcategoria=new SubcategoriaBean();
            detalleDatosSubcategoria=(SubcategoriaBean) datosSubcategorias.elementAt(i);
            // Vaciamos los datos
            modelSubcategorias.addRow(new Object[]{detalleDatosSubcategoria.getNombreSubcategoria(), detalleDatosSubcategoria.getDescripcionSubcategoria(), detalleDatosSubcategoria.getIdSubcategoria()});
         }
      }else{
      modelSubcategorias.addRow(new Object[]{"No existen Subccategorias registradas"});
      }
    }

    // Obtiene tamaño columnas tablas
    public void getTablaListadoSubcategoria(){
        // Vaciamos el modelo con los datos
        tblListaSubcategoria.setModel(modelSubcategorias);
        // Definimos el tamaño de las columnas
        // Nombre subcategoria
        tblListaSubcategoria.getColumnModel().getColumn(0).setPreferredWidth(350);
        // Descripcion
        tblListaSubcategoria.getColumnModel().getColumn(1).setPreferredWidth(200);
        // IdSubcategoria
        tblListaSubcategoria.getColumnModel().getColumn(2).setMinWidth(0);
        tblListaSubcategoria.getColumnModel().getColumn(2).setMaxWidth(0);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        btnAgregarSubcategoria = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        btnModificarSubcategoria = new javax.swing.JButton();
        btnEliminarSubcategoria = new javax.swing.JButton();
        btnCerrar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtSubcategoria = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblListaSubcategoria = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sistemapegasso.SistemaPegassoApp.class).getContext().getResourceMap(frmListadoSubcategoria.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setBackground(resourceMap.getColor("jLabel1.background")); // NOI18N
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setOpaque(true);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        btnAgregarSubcategoria.setFont(resourceMap.getFont("btnAgregarSubcategoria.font")); // NOI18N
        btnAgregarSubcategoria.setIcon(resourceMap.getIcon("btnAgregarSubcategoria.icon")); // NOI18N
        btnAgregarSubcategoria.setText(resourceMap.getString("btnAgregarSubcategoria.text")); // NOI18N
        btnAgregarSubcategoria.setToolTipText(resourceMap.getString("btnAgregarSubcategoria.toolTipText")); // NOI18N
        btnAgregarSubcategoria.setBorderPainted(false);
        btnAgregarSubcategoria.setContentAreaFilled(false);
        btnAgregarSubcategoria.setFocusable(false);
        btnAgregarSubcategoria.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAgregarSubcategoria.setName("btnAgregarSubcategoria"); // NOI18N
        btnAgregarSubcategoria.setPreferredSize(new java.awt.Dimension(36, 36));
        btnAgregarSubcategoria.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAgregarSubcategoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarSubcategoriaActionPerformed(evt);
            }
        });
        jToolBar1.add(btnAgregarSubcategoria);

        btnBuscar.setFont(resourceMap.getFont("btnBuscar.font")); // NOI18N
        btnBuscar.setIcon(resourceMap.getIcon("btnBuscar.icon")); // NOI18N
        btnBuscar.setText(resourceMap.getString("btnBuscar.text")); // NOI18N
        btnBuscar.setToolTipText(resourceMap.getString("btnBuscar.toolTipText")); // NOI18N
        btnBuscar.setBorderPainted(false);
        btnBuscar.setContentAreaFilled(false);
        btnBuscar.setFocusable(false);
        btnBuscar.setName("btnBuscar"); // NOI18N
        btnBuscar.setPreferredSize(new java.awt.Dimension(38, 38));
        jToolBar1.add(btnBuscar);

        btnModificarSubcategoria.setFont(resourceMap.getFont("btnModificarSubcategoria.font")); // NOI18N
        btnModificarSubcategoria.setIcon(resourceMap.getIcon("btnModificarSubcategoria.icon")); // NOI18N
        btnModificarSubcategoria.setText(resourceMap.getString("btnModificarSubcategoria.text")); // NOI18N
        btnModificarSubcategoria.setToolTipText(resourceMap.getString("btnModificarSubcategoria.toolTipText")); // NOI18N
        btnModificarSubcategoria.setBorderPainted(false);
        btnModificarSubcategoria.setContentAreaFilled(false);
        btnModificarSubcategoria.setFocusable(false);
        btnModificarSubcategoria.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnModificarSubcategoria.setName("btnModificarSubcategoria"); // NOI18N
        btnModificarSubcategoria.setPreferredSize(new java.awt.Dimension(36, 36));
        btnModificarSubcategoria.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnModificarSubcategoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarSubcategoriaActionPerformed(evt);
            }
        });
        jToolBar1.add(btnModificarSubcategoria);

        btnEliminarSubcategoria.setIcon(resourceMap.getIcon("btnEliminarSubcategoria.icon")); // NOI18N
        btnEliminarSubcategoria.setText(resourceMap.getString("btnEliminarSubcategoria.text")); // NOI18N
        btnEliminarSubcategoria.setToolTipText(resourceMap.getString("btnEliminarSubcategoria.toolTipText")); // NOI18N
        btnEliminarSubcategoria.setBorderPainted(false);
        btnEliminarSubcategoria.setContentAreaFilled(false);
        btnEliminarSubcategoria.setFocusable(false);
        btnEliminarSubcategoria.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEliminarSubcategoria.setName("btnEliminarSubcategoria"); // NOI18N
        btnEliminarSubcategoria.setPreferredSize(new java.awt.Dimension(36, 36));
        btnEliminarSubcategoria.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btnEliminarSubcategoria);

        btnCerrar.setFont(resourceMap.getFont("btnCerrar.font")); // NOI18N
        btnCerrar.setIcon(resourceMap.getIcon("btnCerrar.icon")); // NOI18N
        btnCerrar.setText(resourceMap.getString("btnCerrar.text")); // NOI18N
        btnCerrar.setToolTipText(resourceMap.getString("btnCerrar.toolTipText")); // NOI18N
        btnCerrar.setBorderPainted(false);
        btnCerrar.setContentAreaFilled(false);
        btnCerrar.setFocusable(false);
        btnCerrar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCerrar.setName("btnCerrar"); // NOI18N
        btnCerrar.setPreferredSize(new java.awt.Dimension(36, 36));
        btnCerrar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnCerrar);

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtSubcategoria.setFont(resourceMap.getFont("txtSubcategoria.font")); // NOI18N
        txtSubcategoria.setText(resourceMap.getString("txtSubcategoria.text")); // NOI18N
        txtSubcategoria.setName("txtSubcategoria"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblListaSubcategoria.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Categoría", "Descripción"
            }
        ));
        tblListaSubcategoria.setName("tblListaSubcategoria"); // NOI18N
        tblListaSubcategoria.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListaSubcategoriaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblListaSubcategoria);
        tblListaSubcategoria.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tblListaSubcategoria.columnModel.title0")); // NOI18N
        tblListaSubcategoria.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tblListaSubcategoria.columnModel.title1")); // NOI18N

        jLabel3.setBackground(resourceMap.getColor("jLabel3.background")); // NOI18N
        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setForeground(resourceMap.getColor("jLabel3.foreground")); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        jLabel3.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 718, Short.MAX_VALUE)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 718, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSubcategoria, javax.swing.GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE)
                .addGap(83, 83, 83))
            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 718, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 694, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSubcategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnModificarSubcategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarSubcategoriaActionPerformed
        // TODO add your handling code here:
        // Evaluamos si selecciono alguna Subcategoria a modificar
        if(!subcategoriaSel.equalsIgnoreCase("")){
        //Cargamos el formulario modificar subcategoria
        frmAltaSubcategoria modificarSubcategoria=new frmAltaSubcategoria(2);
        //Mostramos el formulario
        modificarSubcategoria.setVisible(true);
        //Cambiamos el titulo de la ventana
        modificarSubcategoria.setTitle("Modificar Subcategoria");
        }else{
        // Mensaje al usuario
            JOptionPane.showMessageDialog(null,"Debe Seleccionar una Subcategoria a Modificar, Verifique...","Categorias",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnModificarSubcategoriaActionPerformed

    private void btnAgregarSubcategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarSubcategoriaActionPerformed
        // TODO add your handling code here:
        //Cargamos el formulario alta subcategoria
        altaSubcategoria=new frmAltaSubcategoria(1);
        // Cachamos la accion del boton guardar
        altaSubcategoria.btnGuardar.setActionCommand("newAltaSubcategoria");
        altaSubcategoria.btnGuardar.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
              if(e.getActionCommand().equals("newAltaSubcategoria")){
              altaSubcategoria.altaClickSubcategoria();
              obtSubcategorias();
              }
            }
        });
        //Mostramos el formulario
        altaSubcategoria.setVisible(true);
    }//GEN-LAST:event_btnAgregarSubcategoriaActionPerformed

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        // TODO add your handling code here:
        //Cerramos la ventana
        this.setVisible(false);
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void tblListaSubcategoriaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListaSubcategoriaMouseClicked
        // TODO add your handling code here:
        this.subcategoriaSel=Integer.toString(tblListaSubcategoria.getSelectedRow());
    }//GEN-LAST:event_tblListaSubcategoriaMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmListadoSubcategoria().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarSubcategoria;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnEliminarSubcategoria;
    private javax.swing.JButton btnModificarSubcategoria;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tblListaSubcategoria;
    private javax.swing.JTextField txtSubcategoria;
    // End of variables declaration//GEN-END:variables

}
